<template>
  <div class="contain">
    <div class="contain-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>系统设置</el-breadcrumb-item>
        <el-breadcrumb-item>公告管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="contain-body">
      <div class="body-header">
        <el-form :inline="true" size="mini">
          <el-form-item label="标题">
            <el-input placeholder="请输入标题" v-model="page.title"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="findPage">查 询</el-button>
            <el-button type="primary" icon="el-icon-refresh" @click="reset">刷 新</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="addDialog = true">新 增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="body-table">
        <el-table
            :data="tableData"
            style="width: 100%">
          <el-table-column
              type="index"
              label="序号"
              width="50px"
              align="center"
          >
          </el-table-column>
          <el-table-column
              prop="announcementTitle"
              label="公告标题"
              align="center"
          >
          </el-table-column>
          <el-table-column
              prop="announcementContent"
              label="公告内容"
              align="center"
              show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
              prop="name"
              label="发布人"
              align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.user.telPhone }}
            </template>
          </el-table-column>
          <el-table-column
              prop="updateTime"
              label="操作时间"
              align="center"
          >
          </el-table-column>
          <el-table-column
              label="状态"
              align="center"
          >
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === 1 ? '' : 'danger'" size="mini">
                {{ scope.row.status === 1 ? '启用' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
              label="操作"
              align="center"
          >
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="changeStatus(scope.row.id,scope.row.status)">
                {{ scope.row.status === 1 ? '禁 用' : '启 用' }}
              </el-button>

              <el-button type="text" size="mini" @click="showSee(scope.row)">查 看</el-button>
              <el-button type="text" size="mini" @click="deleteById(scope.row.id)">删 除</el-button>
            </template>
          </el-table-column>


        </el-table>
      </div>
      <div class="body-page">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.pageIndex"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
    </div>
    <!--		新增公告-->
    <el-dialog
        title="新增公告"
        :visible.sync="addDialog"
        width="40%"
    >
      <el-form size="mini" label-width="80px">
        <el-form-item label="公告标题">
          <el-input placeholder="请输入公告标题" v-model="addObj.announcementTitle"></el-input>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input type="textarea"
                    :rows="5" placeholder="请输入公告内容" v-model="addObj.announcementContent"></el-input>
        </el-form-item>
        <el-form-item label="公告状态">
          <el-radio-group v-model="addObj.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="save" size="mini">发 布</el-button>
				<el-button @click="addDialog = false" size="mini">取 消</el-button>
			</span>
    </el-dialog>
    <!--		查看公告-->
    <el-dialog
        title="查看公告"
        :visible.sync="seeDialog"
        width="40%"
    >
      <el-form size="mini" label-width="80px">
        <el-form-item label="公告标题">
          <el-input placeholder="请输入公告标题" v-model="editObj.announcementTitle"></el-input>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input type="textarea"
                    :rows="5" placeholder="请输入公告内容" v-model="editObj.announcementContent"></el-input>
        </el-form-item>
        <el-form-item label="公告状态">
          <el-radio-group v-model="editObj.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="editSubmit" size="mini">发 布</el-button>
				<el-button @click="seeDialog = false" size="mini">取 消</el-button>
			</span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      addDialog: false,
      seeDialog: false,
      page: {
        pageIndex: 1,
        pageSize: 10,
        title: null,
      },
      total: 0,
      addObj: {},
      editObj: {}
    }
  },
  created() {
    this.findPage()
  },
  methods: {
    deleteById(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.post('/admin-announcement/deleteById?id=' + id).then(res => {
          if (res.data.code === 200) {
            this.findPage()
            this.$message.success(res.data.message)
          }
        })
      })
    },
    editSubmit() {
      this.axios.post('/admin-announcement/edit', this.editObj).then(res => {
        if (res.data.code === 200) {
          this.seeDialog = false
          this.findPage()
          this.$message.success(res.data.message)
        }
      })
    },
    showSee(obj) {
      this.editObj = obj
      this.seeDialog = true
    },
    save() {
      this.axios.post('/admin-announcement/save', this.addObj).then(res => {
        if (res.data.code === 200) {
          this.addDialog = false
          this.findPage()
          this.$message.success(res.data.message)
        }
      })
    },
    reset() {
      this.page.pageSize = 10;
      this.page.pageIndex = 1;
      this.page.title = null
      this.findPage()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.findPage()
    },
    handleCurrentChange(val) {
      this.page.pageIndex = val
      this.findPage()
    },
    findPage() {
      this.axios.post('/admin-announcement/findPage', this.page).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data.data
          this.total = res.data.data.total
        }
      })
    },
    changeStatus(id, status) {
      this.$confirm((status === 1 ? '禁用' : '启用') + '该公告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.post('/admin-announcement/changeStatus?id=' + id).then(res => {
          if (res.data.code === 200) {
            this.findPage()
            this.$message.success('操作成功')
          }
        })
      })
    }
  },
}
</script>

<style scoped lang="less">
.contain {
  width: 100%;
  height: 100%;

  .contain-header {
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
  }

  .contain-body {
    width: 100%;
    height: calc(100% - 50px);
    background-color: #fff;
    padding: 1%;
    box-sizing: border-box;

    .body-header {
      width: 100%;
      height: 50px;
    }

    .body-table {
      width: 100%;
      height: calc(100% - 120px);
    }

    .body-page {
      width: 100%;
      height: 70px;
      display: flex;
      align-items: center;
    }
  }
}
</style>
